<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
      offset="50"
    >
      <article-item
        v-for="obj in ArticleList"
        :key="obj.art_id"
        :obj="obj"
        @click.native="$router.push(`/article_detail?aid=${obj.art_id}`)"
        :showX="false"
      ></article-item>
          </van-list>
        </div>
</template>

<script>
import { searchResultAPI } from '@/api'
import ArticleItem from '@/views/Home/components/ArticleItem.vue'
import { timeAgo } from '@/utils/date.js'
export default {
  components: {
    ArticleItem
  },

  name: 'SearchResult',
  data () {
    return {
      ArticleList: [],
      loading: false, // 加载状态
      finished: false, // 全部加载完成状态
      page: 1
    }
  },
  async created () {
    this.getSearchResultFn()
  },
  methods: {
    onLoad () {
      if (this.ArticleList.length > 0) {
        this.page++
        this.getSearchResultFn()
      }
    },
    async getSearchResultFn () {
      const res = await searchResultAPI({
        q: this.$route.params.keywords
      })
      console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
      }
      res.data.data.results.forEach(obj => {
        obj.pubdate = timeAgo(obj.pubdate)
      })
      this.ArticleList = [...this.ArticleList, ...res.data.data.results]

      this.loading = false
    }
  }

}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
